package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.api.content.model.CourseBaseDTO;
import com.xuecheng.api.content.model.qo.QueryCourseModel;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.enums.common.CommonEnum;
import com.xuecheng.common.util.StringUtil;
import com.xuecheng.content.convert.CourseBaseContent;
import com.xuecheng.content.entity.CourseBase;
import com.xuecheng.content.mapper.CourseBaseMapper;
import com.xuecheng.content.service.CourseBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 课程基本信息 服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
public class CourseBaseServiceImpl extends ServiceImpl<CourseBaseMapper, CourseBase> implements CourseBaseService {

    /*
     *   1.是否需要开启事务
     *      由于是查询不许要开始事务
     *   2.判断关键的业务数据
     *     分页数据
     *     条件查询
     *   3.构建查询条件对象
     *   4.根据条件和分页数据进行查询操作
     *   5.封装查询数据
     * */
    @Override
    public PageVO queryCourseList(PageRequestParams params, QueryCourseModel model,Long companyId) {
        //2.判断关键的业务数据
        Long pageNo = params.getPageNo();
        Integer pageSize = params.getPageSize();
        if (pageNo<1){
            pageNo = PageRequestParams.DEFAULT_PAGE_NUM;
        }
        if (pageSize<1){
            pageSize = PageRequestParams.DEFAULT_PAGE_SIZE;
        }
        //3.构建查询条件对象
        LambdaQueryWrapper<CourseBase> wrapper = new LambdaQueryWrapper<>();
        /**
         * 1.课程名称
         *      模糊查询  like
         * 2.审核状态
         *      精确匹配
         */
        if (StringUtil.isNotBlank(model.getCourseName())) {
            wrapper.like(CourseBase::getName, model.getCourseName());
        }
        if (StringUtil.isNotBlank(model.getAuditStatus())) {
            wrapper.eq(CourseBase::getAuditStatus, model.getAuditStatus());
        }
        //添加有效数据的过滤
        wrapper.eq(CourseBase::getStatus, CommonEnum.USING_FLAG.getCode());
        //添加对公司id的查询条件
        wrapper.eq(CourseBase::getCompanyId,companyId);
        //4.创建分页数据
        Page<CourseBase> page = new Page<>(pageNo, pageSize);
        //5.查询数据
        /**
         * page mq提供的分页方法
         *   参数：
         *      1.分页数据对象 Page
         *      2.查询条件对象 QueryWrapper
         */
        Page<CourseBase> pageResult = this.page(page, wrapper);
        //6.封装结果数据
        List<CourseBase> records = pageResult.getRecords();
        //pos转dtos
        List<CourseBaseDTO> courseBaseDTOS = Collections.EMPTY_LIST;
        if (!ObjectUtils.isEmpty(records)){
            courseBaseDTOS = CourseBaseContent.INSTANCE.entity2dtos(records);
        }
        long total = pageResult.getTotal();
        PageVO pageVO = new PageVO(courseBaseDTOS,total,pageNo,pageSize);
        return pageVO;
    }
}
